<template>
	<view class="formClass">
		<view><u-toast ref="uToast" /></view>
		<u-form :model="form" ref="uForm" >
			<u-form-item label="选择应用:" label-width="150rpx" prop="app_name" >
				<u-input v-model="form.app_name" type="select" @click="show=true" border placeholder="请选择已安装应用"/>
				<u-select label-name="app_name" value-name="app_package" v-model="show" :list="appList" @confirm="setAppPackage"></u-select>
			</u-form-item>
			<u-form-item label="应用包名:" label-width="150rpx" prop="package">
				<u-input v-model="form.package_name" border></u-input>
			</u-form-item>
		</u-form>
		<view style="display: flex; justify-content: space-between;">
			<u-button @click="submit()" type="primary" shape="circle" :custom-style="customStyle">提交</u-button>
			<u-button @click="cancel" shape="circle" :custom-style="customStyle">取消</u-button>
		</view>
	</view>
</template>

<script>
import { addAppInside, addWhiteApp, getAppInstall } from '../../../api/userHome';
	export default {
		data() {
			return {
				show: false,
				form: {
					package_name: '',
					app_name: '',
				},
				appList: [],
				customStyle: {
					magin: '0rpx',
					padding: '0rpx',
					width: '200rpx',
					height: '80rpx'
				},
				rules: {
					app_name: [{required: true, message: '请输入应用包名', trigger: 'change'}],
					package_name: [{required: true, message: '请选择应用', trigger: 'blur'}]
				}
			}
		},
		onLoad() {
			this.queryInstallApp()
		},
		methods: {
			// 点击actionSheet回调
			setAppPackage(e) {
				this.form.package_name = e[0].value
				this.form.app_name = e[0].label
			},
			
			queryInstallApp() {
				getAppInstall({
					id: uni.getStorageSync("snId"),
					userId: uni.getStorageSync("userId"),
					pageSize: 999
				}).then(res => {
					this.appList = res.data
				}).catch(err => {
					this.appList = []
				})
			},
			addDomain() {
				if (this.form.content.length > 50) {
					this.$refs.uToast.show({
						title: '限制添加50条',
						type: 'info',
						icon: false
					})
				}
				this.form.content.push({value: ''})
			},
			removeDomain(item) {
				var index = this.form.content.indexOf(item)
				if (index !== -1) {
					this.form.content.splice(index, 1)
				}
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						let addForm = JSON.parse(JSON.stringify(this.form))
						addForm.userId = uni.getStorageSync('userId')
						addForm.id = uni.getStorageSync('snId')
						addForm.last_ip = uni.getStorageSync('Ip')
						addWhiteApp(addForm).then(res => {
							this.$refs.uToast.show({
								title: res.msg,
								type: 'success',
								icon: false
							})
							setTimeout(() => {
								uni.navigateBack()
							}, 500)
						}).catch(err => {
							this.$refs.uToast.show({
								title: err.msg,
								type: 'error',
								icon: false
							})
						})
					}else {
						console.log('验证失败')
					}
				})
			},
			cancel() {
				console.log('取消提交')
			}
			
		}
	}
</script>

<style lang="scss">
	.formClass {
		background-color: #ffffff;
		border-style: solid;
		border-color: #ffffff;
		border-radius: 20rpx;
		margin: 15rpx;
		padding: 10rpx;
	}
</style>